package com.patMall.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.patMall.entity.Store;
import com.patMall.entity.User;
import com.patMall.service.StoreService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 750909637@qq.com
 * @PackageName patMall
 * @Package com.patMall.controller
 * @Date 2024/2/23 0:59
 * @Version 1.0
 */
@RestController
@RequestMapping("store")
public class StoreController {
    @Resource
    private StoreService storeService;

    @PostMapping("getStore")
    public Object getStore(Integer storeId){
        Store store = storeService.getStoreId(storeId);
        Map map = new HashMap();
        if (store != null){
            map.put("msg","success");
            map.put("data",store);
        }else {
            map.put("msg","fail");
        }
        return map;
    }
    @PostMapping("findAllStore")
    public Object findUserInfosByName(int page,int limit){
        PageHelper.startPage(page,limit);
        List<Store> storeList = storeService.findAllStore();
        Page<Store> pageList =(Page<Store>) storeList;
        long count = pageList.toPageInfo().getTotal();
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg", "这是数据传来的！");
        map.put("count", count);
        map.put("data", pageList);
        return map;
    }

    @RequestMapping(value = "/findStoreInfosByName")
    public Object findStoreInfosByName(int page,int limit,String storeName){
        PageHelper.startPage(page,limit);
        List<Store> storeList = storeService.findStoreUsername(storeName);
        Page<Store> pageList =(Page<Store>) storeList;
        long count = pageList.toPageInfo().getTotal();
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg", "这是数据传来的！");
        map.put("count", count);
        map.put("data", pageList);
        return map;
    }

    @PostMapping("updateStore")
    public String updateUser(Store store){
        if (store.getStoreId() != null) {
            int count = storeService.updateStore(store);
            if (count == 1) {
                return "success";
            } else {
                return "fail";
            }
        } else {
            return "fail";
        }
    }
    @PostMapping("getStoreCommodity")
    public Object getStoreCommodity(){
        List<Store> storeList = storeService.findByStoreRandom();

        Map map = new HashMap();
        if (storeList.size() > 0){
            map.put("msg","success");
            map.put("data",storeList);
        }else {
            map.put("msg","fail");
        }
        return map;
    }
}
